/**
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/17 22:47
 */
package org.mechanic.fund.web.product;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mechanic.core.web.MediaTypes;
import org.mechanic.fund.domain.product.ProductNetvalue;
import org.mechanic.fund.service.product.IProductNetvalueService;
import org.mechanic.fund.web.BaseController;
import org.mechanic.fund.web.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 产品净值 API
 *
 * @author mechanic
 * @version 0.0.1
 */
@Api("产品净值接口")
@RestController
@RequestMapping(value = "product/netvalue")
public class ProductNetvalueController extends BaseController {

    private IProductNetvalueService netvalueService;

    @Autowired
    public ProductNetvalueController(IProductNetvalueService netvalueService) {
        this.netvalueService = netvalueService;
    }

    @ApiOperation(value = "返回每类基准信息最新的一条记录的集合")
    @RequestMapping(value = "newests", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public Object newests(
      @ApiParam(value = "页码(从0开始)", required = true)
      @RequestParam(required = true, defaultValue = "0") Integer page) {
        Page<ProductNetvalue> netvaluePage = this.netvalueService.getProductNetvalueNewestList(PageUtil.pageRequest(page));
        return result(netvaluePage);
    }

    @ApiOperation(value = "返回匹配产品编号的产品净值列表")
    @RequestMapping(value = "listByProductId", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public Object list(
      @ApiParam(value = "产品编号", required = true) @RequestParam(required = true) String productId,
      @ApiParam(value = "页码(从0开始)", required = true) @RequestParam(required = true, defaultValue = "0") Integer page) {
        Page<ProductNetvalue> netvaluePage = netvalueService.getProductNetvalueList(productId, super.pageRequest(page));
        return result(netvaluePage);
    }

    @ApiOperation(value = "批量保存产品净值")
    @RequestMapping(value = "save", method = RequestMethod.PUT, consumes = MediaTypes.JSON_UTF_8)
    public Object save(
      @ApiParam(value = "产品净值集合", required = true) @RequestBody(required = true)
        List<ProductNetvalue> productNetvalues) {
        this.netvalueService.save(productNetvalues, super.currUser().getId());
        return result();
    }

    @ApiOperation(value = "按 ID 删除产品净值")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE, produces = MediaTypes.JSON_UTF_8)
    public Object delete(
      @ApiParam(value = "产品净值 ID", required = true) @PathVariable(required = true) String id) {
        this.netvalueService.delete(id);
        return result();
    }
}
